package cn.lystudio.rohm_time_op.controller.webApi;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PageUtil;
import cn.lystudio.rohm_time_op.entity.dto.SecDeptDTO;
import cn.lystudio.rohm_time_op.entity.po.DepartmentsPO;
import cn.lystudio.rohm_time_op.entity.vo.LayuiPageApiResponse;
import cn.lystudio.rohm_time_op.exceptions.JsonException;
import cn.lystudio.rohm_time_op.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@SaCheckLogin
@RequestMapping("/webapi/secDept")
@Slf4j
public class SecDeptController {
    private DepartmentService departmentService;

    @Autowired
    @Lazy
    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @GetMapping("/findAll")
    public LayuiPageApiResponse findAll(Integer page, Integer limit) {
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if (roleList.size() != 1) throw new JsonException(500, "权限异常");
        PageUtil.setFirstPageNo(1);
        List<SecDeptDTO> secDeptList = departmentService.getAll4SecDept();
        if(CollUtil.isEmpty(secDeptList)) return LayuiPageApiResponse.ofSuccessData(null,0);

        return LayuiPageApiResponse.ofSuccessData(CollUtil.page(page, limit, secDeptList), secDeptList.size());
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptEnabled")
    public SaResult uptEnabled(Integer departmentID, Boolean enabled) {
        return SaResult.data(departmentService.uptDeptEnabled(departmentID, enabled));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/delSecDept")
    public SaResult delSecDept(Integer departmentID, Boolean enabled) {
        return SaResult.data(departmentService.delDept(departmentID));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/addSecDept")
    public SaResult addSecDept(@RequestBody DepartmentsPO departmentsPO){
        departmentsPO.setEnabled(true);
        return SaResult.data(departmentService.addDept(departmentsPO));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptSecDept")
    public SaResult uptSecDept(@RequestBody DepartmentsPO departmentsPO){
        return SaResult.data(departmentService.uptDept(departmentsPO));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @GetMapping("/getAllSecDeptName")
    public SaResult getAllSecDeptName(){
        List<SecDeptDTO> all4SecDept = departmentService.getAll4SecDept();
        CollUtil.filter(all4SecDept,SecDeptDTO::getEnabled);
        return SaResult.data(all4SecDept);
    }
}